<template>
  <div class="content-wrapper">
    <div class="content-container">
      <el-page-header :content="title" @back="goBack" />

      <el-table
        v-loading="listLoading"
        height="75vh"
        :data="dataList"
        highlight-current-row
        default-expand-all
        :row-key="getId"
      >
        <el-table-column label="操作人员" prop="userName" width="200px" />

        <el-table-column label="所属部门" prop="userGroupName" width="200px" />
        <el-table-column label="操作总时长" prop="handInTime" width="160px" />
        <el-table-column label="上传总数" prop="handInTime" width="160px" />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-dropdown
              v-if="scope.row.trainId"
              trigger="click"
              class="filter-item"
            >
              <el-button type="text" class="el-icon-more" />
              <el-dropdown-menu slot="dropdown" class="user-dropdown">
                <el-dropdown-item>
                  <el-popover placement="left" width="400" trigger="hover">
                    <el-table
                      class="my-popover-table"
                      :data="scope.row.practiceRecordResponses"
                      :row-key="viewRecordId"
                      @row-click="viewAttachment"
                    >
                      <el-table-column
                        property="practiceDuration"
                        label="实操时长"
                      />
                      <el-table-column property="updateTime" label="更新时间" />
                      <el-table-column label="是否合格" prop="passFlag">
                        <template slot-scope="{ row }">
                          <el-tag
                            v-if="row.passFlag == 1"
                            size="mini"
                            type="success"
                          >合格</el-tag>
                          <el-tag
                            v-else-if="row.passFlag == 2"
                            size="mini"
                            type="danger"
                          >不合格</el-tag>
                          <el-tag v-else size="mini" type="info">未评</el-tag>
                        </template>
                      </el-table-column>
                    </el-table>
                    <span slot="reference"> 实操记录 </span>
                  </el-popover>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page-number.sync="listQuery.pageNumber"
        :page-size.sync="listQuery.pageSize"
        @pagination="getDataList"
      />
      <el-dialog title="查看" :visible.sync="practiceDialog" class="mini">
        <el-form>
          <el-row
            ><el-col :span="22">
              <el-form-item label="操作时长">
                <el-input
                  v-model="practiceDuration"
                  size="medium"
                  type="number"
                  style="margin-bottom: 20px; width: 50%"
                  placeholder="请输入时间，单位秒"
                />
              </el-form-item> </el-col
          ></el-row>
        </el-form>

        <el-upload
          class="upload-demo upload_test"
          :action="uploadUrl"
          :on-preview="filePreview"
          :file-list="fileList"
          list-type="picture"
        />

        <el-radio
          v-model="saveData.passFlag"
          style="margin-top: 10px"
          :label="1"
          >合格</el-radio
        >
        <el-radio v-model="saveData.passFlag" :label="2">不合格</el-radio>
        <el-input
          v-model="saveData.comment"
          style="margin-top: 10px"
          type="textarea"
          maxlength="200"
          placeholder="请输入评语"
        />

        <span slot="footer" class="dialog-footer">
          <el-button @click="practiceDialog = false">取 消</el-button>
          <el-button type="primary" @click="savePracticeRecord()"
            >保 存</el-button
          >
        </span>
      </el-dialog>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
    </div>
    <el-dialog title="查看" :visible.sync="practiceDialog" class="mini">
      <el-form>
        <el-row><el-col :span="22">
          <el-form-item label="实操时长">
            <el-input
              v-model="practiceDuration"
              disabled
              size="medium"
              type="number"
              style="margin-bottom: 20px; width: 50%"
              placeholder="请输入时间，单位秒"
            />
          </el-form-item> </el-col></el-row>
      </el-form>

      <el-upload
      disabled
        class="upload-demo upload_test"
        :action="uploadUrl"
        :on-preview="filePreview"
        :file-list="fileList"
        list-type="picture"
      />

      <el-radio
        v-model="saveData.passFlag"
        style="margin-top: 10px"
        :label="1"
        disabled
      >合格</el-radio>
      <el-radio
        v-model="saveData.passFlag"
        :label="2"
        disabled
      >不合格</el-radio>
      <el-input
        v-model="saveData.comment"
        style="margin-top: 10px"
        type="textarea"
        maxlength="200"
        placeholder="请输入评语"
        disabled
      />

      <span slot="footer" class="dialog-footer">
        <el-button @click="practiceDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogStatus == 'create' ? savePracticeRecord() : saveComment()
          "
        >保 存</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { listRecordByTrain } from '@/api/ts/record.js'
import Pagination from '@/components/Pagination'
import { get } from '@/api/ts/practice'

export default {
  components: { Pagination },
  data() {
    return {
      uploadUrl:
        process.env.VUE_APP_BASE_API + '/api/base/attachment/upload_attachment',
      dataList: [],
      total: 0,
      paperOptions: [],
      userGroupOptions: [],
      listQuery: {
        pageIndex: 0,
        pageNumber: 1,
        withPracticeRecords: true,
        trainId: ''
      },
      listLoading: false,
      recordViewDialog: false, // 查看对话框
      propsData: null,
      practiceDuration: '',
      saveData: {
        practiceRecordId: null,
        comment: '',
        passFlag: 1
      },
      fileList: [],
      practiceDialog: false,
      dialogImageUrl: null,
      dialogVisible: false,
      title: ''
    }
  },
  watch: {},
  mounted() {
    this.listQuery.trainId = this.$route.query.trainId
    this.title = this.$route.query.trainName
    this.getDataList()
  },
  methods: {
    getDataList() {
      listRecordByTrain(this.listQuery).then((res) => {
        this.dataList = res.data
      })
    },
    viewRecord(row, column, event) {
      this.$emit('viewRecord', row.examinationRecordId)
    },
    handleFilter() {},
    // c重新考核
    reloadExamine(row) {
      console.log(row)
    },
    // 考试记录
    testHistory(row) {
      console.log(row)
    },
    getId(row) {
      return row.userId
    },
    viewRecordId(row) {},
    goBack() {
      this.$router.go(-1)
    },
    filePreview(file) {
      // 文件详细查看
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    viewAttachment(row) {
      get({ practiceRecordId: row.practiceRecordId }).then((res) => {
        this.practiceDuration = res.data.practiceDuration
        this.saveData.practiceRecordId = res.data.practiceRecordId
        this.data = res.data
        this.saveData.comment = res.data.comment
        if (res.data.passFlag != 0) {
          this.saveData.passFlag = res.data.passFlag
        }
        this.fileList = []
        if (res.data.attachmentListResult) {
          res.data.attachmentListResult.forEach((item) => {
            var data = {
              name: item.fileName,
              url: item.objectUrl
            }
            this.fileList.push(data)
            this.practiceDialog = true
            this.dialogStatus = 'update'
          })
        }
      })
    }
  }
}
</script>
<style scope>
.view_record .el-dialog .el-dialog__header {
  padding: 0px !important;
}
.view_record .el-dialog .el-dialog__body {
  padding: 30px 0px !important;
}
.my-popover-table {
  height: unset;
}
</style>
